package com.lenovo.smarttraffic.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.Log;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.database.dao.DaoManager;
import com.lenovo.smarttraffic.database.dao.TitleStrBean;
import com.lenovo.smarttraffic.database.dao.TitleStrBeanDao;
import com.lenovo.smarttraffic.ui.fragment.FragmentFactory;
import com.lenovo.smarttraffic.util.CommonUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class BasePagerAdapter extends FragmentStatePagerAdapter {
    public String TAG = "BasePagerAdapter";
    //使用无参构造方法 初始化   用fg工厂里面的map数组  不用fragmentList
    //tabLayout  和  viewPager  联动
    //由tabLayout  决定加载   几个页面
    //外加 是工厂模式     所以更新viewpager时只需要更新tabLayout
    //List<String>  比  String数组  更好使用

    private List<String> tabArr;
    private List<TitleStrBean> list;

    //初始化用得无参方法
    public BasePagerAdapter(FragmentManager fm) {
        super(fm);
        initTitleList();
//   标题名称   从资源文件中获取
    }

    //数据库中获取   TitleList
    private void initTitleList() {
        TitleStrBeanDao titleStrBeanDao = DaoManager.getInstance().getDaoSession().getTitleStrBeanDao();
        list = titleStrBeanDao.queryBuilder().list();
        Log.e(TAG, "数据库内容1: " + list.size());
        List<String> tabArrlist = new ArrayList<>();
        if (list.size() == 0) {
            titleStrBeanDao.insert(new TitleStrBean(null, "推荐"));
            titleStrBeanDao.insert(new TitleStrBean(null, "热点"));
            titleStrBeanDao.insert(new TitleStrBean(null, "科技"));
            titleStrBeanDao.insert(new TitleStrBean(null, "汽车资讯"));
            list = titleStrBeanDao.queryBuilder().list();
            Log.e(TAG, "数据库内容2: " + list.size());
        }

        for (int i = 0; i < list.size(); i++) {
            //  tabArr.add(list.get(i).getTitle()+" ");
            tabArrlist.add(list.get(i).getTitle());
        }
        this.tabArr = tabArrlist;
    }

    public BasePagerAdapter(FragmentManager fm, String[] titles) {
        super(fm);
        //  this.fragmentList = fragmentList;
        // this.titleList = new ArrayList<>(Arrays.asList(titles));
        this.tabArr = Arrays.asList(titles);
    }

    public BasePagerAdapter(FragmentManager fm, List<String> titles) {
        super(fm);
        //  this.fragmentList = fragmentList;
        // this.titleList = new ArrayList<>(Arrays.asList(titles));
        this.tabArr = titles;
    }

    @Override
    public Fragment getItem(int position) {
        //只是初始化用到
        // 现在觉得不仅是初始化用到了
        Log.e("FragmentFactoryUsed", "getItem: ");
        return FragmentFactory.getInstance().getFragment(position);

    }

    @Override
    public int getCount() {
        return tabArr.size();
    }

    //    此方法用来显示tab上的名字  和viewpager联动
    //    setupWithViewPager
    @Override
    public CharSequence getPageTitle(int position) {
        return tabArr.get(position);
    }


    //这个不会     不是框架自带的吧
    // 最简单解决 notifyDataSetChanged() 页面不刷新问题的方法
    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

}
